<template>
  <div class="home-category" @mouseleave="current = []">
    <ul class="menu">
      <li
        v-for="i in list"
        :key="i"
        @mouseenter="mouseEvent(i)"
        :class="{ active: current.id === i.id }"
      >
        <RouterLink to="/">{{ i.name }}</RouterLink>
        <span v-if="i.children">
          <RouterLink
            to="/"
            v-for="(item, index) in i.children"
            :key="item.id"
            v-show="index < 2"
          >
            {{ item.name }}
          </RouterLink>
        </span>

        <span v-else>
          <XtxSkeleton
            :width="Math.random() * 15 + 65 + 'px'"
            height="20px"
            style="margin-right:5px"
          ></XtxSkeleton>
          <XtxSkeleton
            :width="Math.random() * 15 + 65 + 'px'"
            height="20px"
            style="margin-right:5px"
          ></XtxSkeleton>
        </span>
      </li>
    </ul>

    <!-- 弹层 -->
    <div class="layer">
      <h4>
        {{ current.id !== 'brand' ? '分类' : '品牌' }}推荐
        <small>根据您的购买或浏览记录推荐</small>
      </h4>
      <!-- 分类模板 -->
      <ul v-if="current.id !== 'brand'">
        <li v-for="i in current.goods" :key="i.id">
          <RouterLink to="/">
            <img :src="i.picture" alt="" />
            <div class="info">
              <p class="name ellipsis-2">{{ i.name }}</p>
              <p class="desc ellipsis">{{ i.desc }}</p>
              <p class="price"><i>¥</i>{{ i.price }}</p>
            </div>
          </RouterLink>
        </li>
      </ul>
      <!-- 品牌模块 -->
      <ul v-else>
        <li class="brand" v-for="i in brandSub" :key="i.id">
          <RouterLink to="/">
            <img :src="i.picture" alt="" />
            <div class="info">
              <p class="place">
                <i class="iconfont icon-dingwei"></i>{{ i.place }}
              </p>
              <p class="name ellipsis">{{ i.name }}</p>
              <p class="desc ellipsis-2">{{ i.desc }}</p>
            </div>
          </RouterLink>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { computed, ref } from 'vue'
import { useStore } from 'vuex'
export default {
  name: 'HomeCategory',
  setup () {
    const store = useStore()
    // 右侧menu列表追加品牌分类
    const list = computed(() => {
      const _arr = [
        ...store.state.category.list,
        {
          id: 'brand',
          name: '品牌',
          children: [{ id: 'brand_001', name: '品牌推荐' }]
        }
      ]
      return _arr
    })

    // 当前选中的分类
    const current = ref('')
    // 品牌子列表
    const brandSub = ref([])

    async function mouseEvent (item) {
      current.value = item
      if (item.id === 'brand' && brandSub.value.length === 0) {
        const result = await store.dispatch('home/getBrandList')
        brandSub.value = result.slice(0, 9)
      }
    }

    return { list, mouseEvent, current, brandSub }
  }
}
</script>

<style scoped lang="less">
.home-category {
  width: 250px;
  height: 500px;
  background: rgba(0, 0, 0, 0.8);
  position: relative;
  z-index: 99;
  .menu {
    li {
      padding-left: 40px;
      height: 50px;
      line-height: 50px;
      //   &:hover {
      //     background: @xtxColor;
      //   }
      &.active {
        background: @xtxColor;
      }
      a {
        margin-right: 4px;
        color: #fff;
      }
      > a {
        font-size: 16px;
      }
    }
  }
  .layer {
    width: 990px;
    height: 500px;
    background: rgba(255, 255, 255, 0.8);
    position: absolute;
    left: 250px;
    top: 0;
    display: none;
    padding: 0 15px;
    h4 {
      font-size: 20px;
      font-weight: normal;
      line-height: 80px;
      small {
        font-size: 16px;
        color: #666;
      }
    }
    ul {
      display: flex;
      flex-wrap: wrap;
      li {
        width: 310px;
        height: 120px;
        margin-right: 15px;
        margin-bottom: 15px;
        border: 1px solid #eee;
        border-radius: 4px;
        background: #fff;
        &:nth-child(3n) {
          margin-right: 0;
        }
        a {
          display: flex;
          width: 100%;
          height: 100%;
          align-items: center;
          padding: 10px;
          &:hover {
            background: #e3f9f4;
          }
          img {
            width: 95px;
            height: 95px;
          }
          .info {
            padding-left: 10px;
            line-height: 24px;
            width: 190px;
            .name {
              font-size: 16px;
              color: #666;
            }
            .desc {
              color: #999;
            }
            .price {
              font-size: 22px;
              color: @priceColor;
              i {
                font-size: 16px;
              }
            }
          }
        }
      }
    }
  }
  &:hover {
    .layer {
      display: block;
    }
  }
}
</style>
